package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.module.common.domain.City;
import com.rpframework.module.common.service.CityService;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.Reportactivity;
import com.rpframework.website.device.domain.Room;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.RoomService;
/**
 * 注册流程
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/processes")
public class ApiRegistAct extends BaseAct{
		@Resource CityService cityService;
		@Resource EstateService estateService;
		@Resource RoomService roomService;
	/**
	 * 1.有小区的城市列表
	 * 2.某城市的所有小区列表
	 * 3.某小区的所有栋
	 * 4.某栋的所有单元
	 * 5.某单元的所有楼号
	 * 6.将地址带着注册成功，生成一张地址表数据，些数据id加到用户的默认地址id上。
	 */
	/**
	 * 只查开通小区的城市 
	 * @return
	 */
	@RequestMapping(value="/cityList",produces = "application/json; charset=utf-8")
	//只查开通小区的城市 
	public @ResponseBody String getCityListHasVillage(
			@RequestParam(value="name",required=false) String name,
			@RequestParam(value = "callback",required = false ) String callback
			){
		JsonObject json = new JsonObject();
		List<City> list = cityService.getCityListHasVillage(name);
		if(list!=null && list.size()>0){
			json.addProperty("count", list.size());
		}else{
			json.addProperty("count", 0);
		}
		JsonArray array = new JsonArray();
		for(City li : list){
			JsonObject jObj = new JsonObject();
			array.add(jObj);
			jObj.addProperty("code", li.getCode());
			jObj.addProperty("name", li.getName());
		}
		json.add("data", array);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/**
	 * 根据code查询城市
	 * @param code
	 * @param callback
	 * @return
	 */
	//查询某城市的小区
	@RequestMapping(value="/estateList",produces = "application/json; charset=utf-8")
	public @ResponseBody String getEstateListByCity(
			@RequestParam(value="code",required=false) String code,
			@RequestParam(value = "callback",required = false ) String callback
			){
		JsonObject json = new JsonObject();
		if(StringUtils.isBlank(code)){
			json.addProperty("code", 500);
			json.addProperty("msg", "参数错误");
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		List<Estate> list = estateService.getListByCityCode(code);
		JsonArray array = new JsonArray();
		for(Estate li : list){
			JsonObject jObj = new JsonObject();
			array.add(jObj);
			jObj.addProperty("id", li.getId());
			jObj.addProperty("name", li.getName());
			jObj.addProperty("address", li.getAddress());
		}
		json.add("data", array);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	/*根据小区查询房源列表*/
	//查询某城市的小区
		@RequestMapping(value="/roomList",produces = "application/json; charset=utf-8")
		public @ResponseBody String getEstateListByCity(
				@RequestParam(value="pager",required=false) Pager<Room> pager,//物业小区id
				@RequestParam(value="relativeId",required=false) Integer relativeId,//物业小区id
				@RequestParam(value="roomZ",required=false) String roomZ,//幢
				@RequestParam(value="roomDy",required=false) String roomDy,//单元 
				@RequestParam(value="roomNo",required=false) String roomNo,//楼
				@RequestParam(value = "callback",required = false ) String callback
				){
			JsonObject json = new JsonObject();
			if(pager==null){
				pager=new Pager<Room>();
			}
			if(NumberUtils.isNotValid(relativeId)){
				json.addProperty("code", 500);
				json.addProperty("msg", "参数错误");
				if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
				return ""+json ;
			}
			pager.getSearchMap().put("relativeId", relativeId.toString());
			if(StringUtils.isNotBlank(roomZ))pager.getSearchMap().put("roomZ", roomZ);
			if(StringUtils.isNotBlank(roomZ))pager.getSearchMap().put("roomDy", roomDy);
			if(StringUtils.isNotBlank(roomZ))pager.getSearchMap().put("roomNo", roomNo);
			roomService.getPager(pager);
			JsonArray array = new JsonArray();
			for(Room li : pager.getItemList()){
				JsonObject jObj = new JsonObject();
				array.add(jObj);
				jObj.addProperty("id", li.getId());
				jObj.addProperty("roomZ", li.getRoomZ()+" 幢");
				jObj.addProperty("roomDy", li.getRoomDy()+"单元");
				jObj.addProperty("roomNo", li.getRoomNo()+" 室");
			}
			json.add("data", array);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
	
}
